package com.hamilton.erik.pt.server.resource;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.hamilton.erik.pt.core.dao.NoteDAO;
import com.hamilton.erik.pt.core.dao.factory.DAOFactory;
import com.hamilton.erik.pt.core.dao.factory.DAOFactory.DAOFactoryType;
import com.hamilton.erik.pt.core.model.EnclosureMaintenance;
import com.hamilton.erik.pt.core.model.Note;
import com.hamilton.erik.pt.server.util.RestUtil;

@Path("/enclosure/maintenance")
public class EnclosureMaintenanceResource {
	
	@GET
	@Path("/retrieve/all/{enclosureId}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<EnclosureMaintenance> getAllEnclosureMaintenance(@PathParam("enclosureId") long enclosureId) {
		List<EnclosureMaintenance> enclosureMaintenance = new ArrayList<EnclosureMaintenance>();
		try {
			enclosureMaintenance = DAOFactory.getDAOFactory(DAOFactoryType.H2)
												.getEnclosureMaintenanceDAO()
													.getAllEnclosureMaintenance(
															enclosureId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return enclosureMaintenance;
	}
	
	@GET
	@Path("/retrieve/notes/{enclosureMaintenanceId}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Note> getNotes(@PathParam("enclosureMaintenanceId") long enclosureMaintenanceId) {
		List<Note> notes = new ArrayList<Note>();
		try {
			notes = DAOFactory.getDAOFactory(DAOFactoryType.H2)
									.getNoteDAO().getAllNotes(
										enclosureMaintenanceId,
										NoteDAO.ENCLOSURE_MAINTENANCE_NOTES_DB_TABLE);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return notes;
	}
	
	@PUT
	@Path("/upsert")
	@Produces(MediaType.APPLICATION_JSON)
	public String upsert(@FormParam("id") Long id,
						@NotNull @FormParam("enclosureId") Long enclosureId,
						@NotNull @FormParam("action") String action,
						@NotNull @FormParam("timestamp") String timestamp) {

		EnclosureMaintenance enclosureMaintenance = new EnclosureMaintenance();
		enclosureMaintenance.setId(id);
		enclosureMaintenance.setEnclosureId(enclosureId);
		enclosureMaintenance.setAction(action);
		enclosureMaintenance.setTimestamp(timestamp);
		
		try {
			int returnValue = -1;
			if(id == null) {
				returnValue = DAOFactory.getDAOFactory(DAOFactoryType.H2)
								.getEnclosureMaintenanceDAO()
									.insertEnclosureMaintenance(enclosureMaintenance);
			}
			else {
				returnValue = DAOFactory.getDAOFactory(DAOFactoryType.H2)
								.getEnclosureMaintenanceDAO()
									.updateEnclosureMaintenance(enclosureMaintenance);
			}
			
			if(returnValue <= 0) {
				return RestUtil.getInstance().getSuccess();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}
	
	@PUT
	@Path("/upsert/note")
	@Produces(MediaType.APPLICATION_JSON)
	public String upsertNote(@FormParam("id") Long id,
						@NotNull @FormParam("referenceId") Long enclosureMaintenanceId,
						@NotNull @FormParam("title") String title,
						@NotNull @FormParam("body") String body,
						@FormParam("date") String date) {

		Note note = new Note();
		note.setId(id);
		note.setReferenceId(enclosureMaintenanceId);
		note.setTitle(title);
		note.setNote(body);
		note.setDate(date);
		
		try {
			int returnValue = -1;
			if(id == null) {
				returnValue = DAOFactory.getDAOFactory(DAOFactoryType.H2)
								.getNoteDAO()
									.insertNote(
											note, 
											NoteDAO.ENCLOSURE_MAINTENANCE_NOTES_DB_TABLE);
			}
			else {
				returnValue = DAOFactory.getDAOFactory(DAOFactoryType.H2)
						.getNoteDAO()
							.updateNote(
								note, 
								NoteDAO.ENCLOSURE_MAINTENANCE_NOTES_DB_TABLE);
			}
			
			if(returnValue <= 0) {
				return RestUtil.getInstance().getSuccess();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}
	
	@DELETE
	@Path("/delete/{enclosureMaintenanceId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteEnclosureMaintenance(@PathParam("enclosureMaintenanceId") Long enclosureMaintenanceId) {
		boolean success;
		try {
			success = DAOFactory.getDAOFactory(DAOFactoryType.H2)
						.getEnclosureMaintenanceDAO().deleteEnclosureMaintenance(enclosureMaintenanceId);
			
			if(success) {
				return RestUtil.getInstance().getFailure();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}
	
	@DELETE
	@Path("/delete/all/{enclosureId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteAllEnclosureMaintenance(@PathParam("enclosureId") Long enclosureId) {
		boolean success;
		try {
			success = DAOFactory.getDAOFactory(DAOFactoryType.H2)
						.getEnclosureMaintenanceDAO().deleteAllEnclosureMaintenance(enclosureId);
			
			if(success) {
				return RestUtil.getInstance().getFailure();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}

	@DELETE
	@Path("/delete/note/{noteId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteNote(@PathParam("noteId") Long noteId) {
		
		try {
			boolean success = DAOFactory.getDAOFactory(DAOFactoryType.H2)
								.getNoteDAO()
									.deleteNote(
										noteId, 
										NoteDAO.ENCLOSURE_MAINTENANCE_NOTES_DB_TABLE);
			
			if(success) {
				return RestUtil.getInstance().getSuccess();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}
	
	@DELETE
	@Path("/delete/all/notes/{referenceId}")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteAllNote(@PathParam("referenceId") Long referenceId) {
		
		try {
			boolean success = DAOFactory.getDAOFactory(DAOFactoryType.H2)
								.getNoteDAO()
									.deleteAllNotes(
										referenceId, 
										NoteDAO.ENCLOSURE_MAINTENANCE_NOTES_DB_TABLE);
			
			if(success) {
				return RestUtil.getInstance().getSuccess();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return RestUtil.getInstance().getFailure();
	}
}
